package com.demo.mysql.controller;

import com.demo.mysql.base.DataSourceSelector;
import com.demo.mysql.config.MyBatisConfig;
import com.demo.web.controller.BaseController;
import com.demo.mysql.dao.OrderDao;
import com.demo.mysql.model.Order;
import com.demo.web.model.HttpResponse;
import com.demo.web.model.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class OrderController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private OrderDao orderDao;

    @RequestMapping(value = "/insertOrder", method = {RequestMethod.GET, RequestMethod.POST})
    public HttpResponse<String> insertOrder(@RequestBody Request requestBody) {

        Integer userId = requestBody.getUserId();
        Long orderId = requestBody.getOrderId();
        Integer status = requestBody.getStatus();
        Order order = new Order();
        order.setOrderId(orderId);
        order.setUserId(userId);
        order.setStatus(status);
        orderDao.insert(order);
        return HttpResponse.ok("insert ok");
    }

    @RequestMapping(value = "/selectOrder", method = {RequestMethod.GET, RequestMethod.POST})
    public HttpResponse<List<Order>> selectOrder(@RequestBody Request requestBody) {

        Integer userId = requestBody.getUserId();
        Long orderId = requestBody.getOrderId();
        Integer status = requestBody.getStatus();
        List<Order> list = orderDao.select();
        return HttpResponse.ok(list);
    }

    @RequestMapping(value = "/insertOrder2", method = {RequestMethod.GET, RequestMethod.POST})
    public HttpResponse<String> insertOrder2(@RequestBody Request requestBody) {

        Integer userId = requestBody.getUserId();
        Long orderId = requestBody.getOrderId();
        Integer status = requestBody.getStatus();
        Order order = new Order();
        order.setOrderId(orderId);
        order.setUserId(userId);
        order.setStatus(status);
        DataSourceSelector.set(MyBatisConfig.DB02);
        orderDao.insert(order);
        return HttpResponse.ok("insert ok");
    }

    @RequestMapping(value = "/selectOrder2", method = {RequestMethod.GET, RequestMethod.POST})
    public HttpResponse<List<Order>> selectOrder2(@RequestBody Request requestBody) {

        Integer userId = requestBody.getUserId();
        Long orderId = requestBody.getOrderId();
        Integer status = requestBody.getStatus();
        DataSourceSelector.set(MyBatisConfig.DB02);
        List<Order> list = orderDao.select();
        return HttpResponse.ok(list);
    }


}
